package h0805;

import java.util.HashMap;
import java.util.Scanner;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description TLV解析
 * @date 2024/8/5 12:33
 */
public class TLVAnalysis {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String tlv = sc.nextLine();
        int n = sc.nextInt();
        int[] tags = new int[n];
        for (int i = 0; i < n; i++) {
            tags[i] = sc.nextInt();
        }
        getRes(tags, tlv);
    }

    private static void getRes(int[] tags, String tlv) {
        HashMap<Integer, Integer[]> tagMap = new HashMap<>();
        for (int i = 0; i < tlv.length(); i++) {
            //tag
            String tag_val = tlv.substring(i, i + 2);
            int tag = Integer.parseInt(tag_val, 16);
            i += 2;
            //length
            String len_val = tlv.substring(i, i + 2);
            int len = Integer.parseInt(len_val, 16);
            i += 2;
            int valueOffset = (i + 1) / 2;
            //TODO 检验是否合法, 理论结束位置i + 2 * len-1 最后一个元素索引
            i = i + 2 * len-1;
            if (i > tlv.length()) break; //索引超出，证明输入的不合法
            tagMap.put(tag, new Integer[]{len, valueOffset});
        }

        for (int tag : tags) {
            if (tagMap.containsKey(tag)) {
                Integer[] arr = tagMap.get(tag);
                System.out.println(arr[0] + " " + arr[1]);
            } else {
                System.out.println("0" + " " + "0");
            }
        }
    }
}
